//基础
const express = require('express');
const path= require('path');
const app = express();
const mysql = require('mysql');
//准备
app.use(express.static(path.join(__dirname,"static")));
app.set('view engine','ejs')
app.set('views',path.join(__dirname,'static'));
//数据库
var db = mysql.createConnection({
    host:'localhost',
    user:"root",
    password:'root',
    database:'users',
    port:'3306'
});

//代码
app.get("/",(req,res)=>{
    db.query("select * from user",(err,rows,field)=>{
        if(err){
            console.log(err);
        }else{
            res.render('index',{rows});
        }
    });
   
})
app.get("/add",(req,res)=>{
    db.query("insert into user(name,info) values('"+req.query.name+"','"+req.query.info+"')",(err,rows)=>{
        if(err){
            console.log(err);
        }
    });
    res.redirect('/');
})
app.listen("3000",()=>{
    console.log("http://localhost:3000");
})